package system

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	httpfacade "github.com/hulutech-web/http_result"
	"goravel/app/http/requests"
	"goravel/app/models/systemEntity"
)

type SystemAttachmentCategoryController struct {
	//Dependent services
}

func NewSystemAttachmentCategoryController() *SystemAttachmentCategoryController {
	return &SystemAttachmentCategoryController{
		//Inject services
	}
}

// Index 分页查询，支持搜索，路由参数?name=xxx&pageSize=1&currentPage=1&sort=xxx&order=xxx,等其他任意的查询参数
// @Summary      分页查询
// @Description  分页查询
// @Tags         SystemAttachmentCategoryController
// @Accept       json
// @Produce      json
// @Id SystemAttachmentCategoryIndex
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param  name  query  string  false  "name"
// @Param  pageSize  query  string  false  "pageSize"
// @Param  currentPage  query  string  false  "currentPage"
// @Param  sort  query  string  false  "sort"
// @Param  order  query  string  false  "order"
// @Success 200 {string} json {}
// @Router       /api/system_attachment_category [get]
func (r *SystemAttachmentCategoryController) Index(ctx http.Context) http.Response {
	SystemAttachmentCategorys := []systemEntity.SystemAttachmentCategory{}
	queries := ctx.Request().Queries()
	res, err := httpfacade.NewResult(ctx).SearchByParams(queries, nil).ResultPagination(&SystemAttachmentCategorys)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "", err.Error())
	}
	return res
}

// List 列表查询
// @Summary      列表查询
// @Description  列表查询
// @Tags         SystemAttachmentCategoryController
// @Accept       json
// @Produce      json
// @Id SystemAttachmentCategoryList
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} json {}
// @Router       /api/system_attachment_category/list [get]
func (r *SystemAttachmentCategoryController) List(ctx http.Context) http.Response {
	SystemAttachmentCategorys := []systemEntity.SystemAttachmentCategory{}
	queries := ctx.Request().Queries()
	return httpfacade.NewResult(ctx).SearchByParams(queries, nil).Success("", SystemAttachmentCategorys)
}
func (r *SystemAttachmentCategoryController) Show(ctx http.Context) http.Response {
	id := ctx.Request().RouteInt("id")
	SystemAttachmentCategory := systemEntity.SystemAttachmentCategory{}
	facades.Orm().Query().Model(&systemEntity.SystemAttachmentCategory{}).Where("id = ?", id).First(&SystemAttachmentCategory)
	return httpfacade.NewResult(ctx).Success("", SystemAttachmentCategory)
}

// Store 新增
// @Summary      新增
// @Description  新增
// @Tags         SystemAttachmentCategoryController
// @Accept       json
// @Produce      json
// @Id SystemAttachmentCategoryStore
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param SystemAttachmentCategoryData body requests.SystemAttachmentCategoryRequest true "数据"
// @Success 200 {string} json {}
// @Router       /api/admin/system_attachment_category [post]
func (r *SystemAttachmentCategoryController) Store(ctx http.Context) http.Response {
	var SystemAttachmentCategoryRequest requests.SystemAttachmentCategoryRequest
	errors, err := ctx.Request().ValidateRequest(&SystemAttachmentCategoryRequest)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据错误", err.Error())
	}
	if errors != nil {
		return httpfacade.NewResult(ctx).ValidError("", errors.All())
	}
	SystemAttachmentCategory := systemEntity.SystemAttachmentCategory{
		Name: SystemAttachmentCategoryRequest.Name,
	}
	//todo add request values
	facades.Orm().Query().Model(&systemEntity.SystemAttachmentCategory{}).Create(&SystemAttachmentCategory)
	return httpfacade.NewResult(ctx).Success("创建成功", nil)
}

// Update
// @Summary      更新
// @Description  更新
// @Tags         SystemAttachmentCategoryController
// @Accept       json
// @Produce      json
// @Id SystemAttachmentCategoryUpdate
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param SystemAttachmentCategoryData body requests.SystemAttachmentCategoryRequest true "数据"
// @Success 200 {string} json {}
// @Router       /api/admin/system_attachment_category/{id} [put]
func (r *SystemAttachmentCategoryController) Update(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	SystemAttachmentCategory := systemEntity.SystemAttachmentCategory{}
	facades.Orm().Query().Model(&systemEntity.SystemAttachmentCategory{}).Where("id=?", id).Find(&SystemAttachmentCategory)
	var SystemAttachmentCategoryRequest requests.SystemAttachmentCategoryRequest
	errors, err := ctx.Request().ValidateRequest(&SystemAttachmentCategoryRequest)
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据错误", err.Error())
	}
	if errors != nil {
		return httpfacade.NewResult(ctx).ValidError("", errors.All())
	}
	//todo add request values
	SystemAttachmentCategory = systemEntity.SystemAttachmentCategory{
		Name: SystemAttachmentCategoryRequest.Name,
	}
	facades.Orm().Query().Model(&systemEntity.SystemAttachmentCategory{}).Where("id=?", id).Update(&SystemAttachmentCategory)
	return httpfacade.NewResult(ctx).Success("修改成功", nil)
}

// Destroy 删除
// @Summary      删除
// @Description  删除
// @Tags         SystemAttachmentCategoryController
// @Accept       json
// @Produce      json
// @Id SystemAttachmentCategoryDestroy
// @Security ApiKeyAuth
// @Success 200 {string} json {}
// @Router       /api/admin/system_attachment_category/{id} [delete]
func (r *SystemAttachmentCategoryController) Destroy(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	var cate systemEntity.SystemAttachmentCategory
	facades.Orm().Query().Model(&systemEntity.SystemAttachmentCategory{}).Where("id=?", id).With("SystemAttachments").First(&cate)
	if len(cate.SystemAttachments) > 0 {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "请先删除该分类下的文件", nil)
	}
	facades.Orm().Query().Model(&systemEntity.SystemAttachmentCategory{}).Where("id=?", id).Delete(&systemEntity.SystemAttachmentCategory{})
	return httpfacade.NewResult(ctx).Success("删除成功", nil)
}

// Option 选项
// @Summary      选项
// @Description  选项
// @Tags         SystemAttachmentCategoryController
// @Accept       json
// @Produce      json
// @Id SystemAttachmentCategoryOption
// @Security ApiKeyAuth
// @Success 200 {string} json {}
// @Router       /api/admin/system_attachment_category/option [get]
func (r *SystemAttachmentCategoryController) Option(ctx http.Context) http.Response {
	SystemAttachmentCategorys := []systemEntity.SystemAttachmentCategory{}
	queries := ctx.Request().Queries()
	res, _ := httpfacade.NewResult(ctx).SearchByParams(queries, nil).List(&SystemAttachmentCategorys)
	return res
}

func (r *SystemAttachmentCategoryController) Inner(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	SystemAttachments := []systemEntity.SystemAttachment{}
	queries := ctx.Request().Queries()
	if id != "" {
		res, _ := httpfacade.NewResult(ctx).SearchByParams(queries, map[string]interface{}{
			"pid": id,
		}).ResultPagination(&SystemAttachments)
		return res
	} else {
		res, _ := httpfacade.NewResult(ctx).SearchByParams(queries, map[string]interface{}{}).ResultPagination(&SystemAttachments)
		return res
	}
}
